﻿var zombies = zombies || {};

zombies.AdvertiseTop = function () {
    var self = this;

    self.Loaded = ko.observable(false);

    self.TopViews = ko.observableArray();
    self.TopLikes = ko.observableArray();
    self.TopNews = ko.observableArray();
};

zombies.TopItem = function (item) {
    var self = this;

    self.Link = '../home/Advertise/' + item.AdvId;
    self.HtmlTag = "<img src='../" + item.PreviewImg + "' style='width: 150px; height: 150px;'><h6>" + item.Title + "</h6><h6>Views: " + item.Views + ". Likes: " + item.Likes + "</h6>";
}

zombies.TopNew = function (item) {
    var self = this;

    self.Link = '../home/Advertise/' + item.AdvId;
    self.HtmlTag = "<img src='../" + item.PreviewImg + "' style='width: 250px; height: 150px;'><h4>" + item.Title + "</h4><h4>Views: " + item.Views + ". Likes: " + item.Likes + "</h4>";
}

var load = function () {

    advertiseTopViewModel.TopViews.removeAll();

    $.ajax({
        url: '/api/Recommend/GetTOPView',
        type: 'GET',
        contentType: "application/json; charset=utf-8",
        dataType: "json"
    }).done(function (items) {
        $.each(items, function (idx, item) {
            advertiseTopViewModel.TopViews.push(new zombies.TopItem(item));
        });
    }).fail(function (request) {
        advertiseTopViewModel.Loaded(false);
    });

    advertiseTopViewModel.TopLikes.removeAll();

    $.ajax({
        url: '/api/Recommend/GetTOPLike',
        type: 'GET',
        contentType: "application/json; charset=utf-8",
        dataType: "json"
    }).done(function (items) {
        $.each(items, function (idx, item) {
            advertiseTopViewModel.TopLikes.push(new zombies.TopItem(item));
        });
    }).fail(function (request) {
        advertiseTopViewModel.Loaded(false);
    });

    advertiseTopViewModel.TopNews.removeAll();

    $.ajax({
        url: '/api/Recommend/GetTOPNew',
        type: 'GET',
        contentType: "application/json; charset=utf-8",
        dataType: "json"
    }).done(function (items) {
        $.each(items, function (idx, item) {
            advertiseTopViewModel.TopNews.push(new zombies.TopNew(item));
        });
    }).fail(function (request) {
        advertiseTopViewModel.Loaded(false);
    });
};

var advertiseTopViewModel;
$(document).ready(function () {
    advertiseTopViewModel = new zombies.AdvertiseTop();
    load();
    ko.applyBindings(advertiseTopViewModel, document.getElementById("my-container"));
});